package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.OperateLogAnnotation;
import com.itheima.exception.TliasException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private StudentService studentService;


    public PageBean selectClazzDatas(ClazzParam clazzParam) {

        // 分页查询
        PageHelper.startPage(clazzParam.getPage(), clazzParam.getPageSize());


        // 执行SQL
        List<Clazz> classList = clazzMapper.selectClazzDatas(clazzParam);

        LocalDate nowDate = LocalDate.now();
        classList.forEach((item)->{
            LocalDate endDate = item.getEndDate();
            if (nowDate.isAfter(endDate)) {
                item.setStatus("已结课");
            } else if (nowDate.isBefore(endDate)) {
                item.setStatus("未开班");
            } else {
                item.setStatus("在读中");
            }
        });

        Page page = (Page) classList;
        long total = page.getTotal();
        List result = page.getResult();
        PageBean pageBean = new PageBean<>(total, result);
        return pageBean;
    }

    // 表关系是删除
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperateLogAnnotation
    public void delClazzById(Integer id) throws Exception {
        System.out.println("id1111111 = " + id);
        // 删除之前 先看看这个班级里面还有学生么，有学生就不能删除
      List<Student> list = studentService.selectStudentByClazzId(id);

        System.out.println("list = " + list);

      if(!(CollectionUtils.isEmpty(list))){

          throw new TliasException("当前班级中还存在学生,请先删除掉对应的学生");
      }
        clazzMapper.delClazzById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperateLogAnnotation
    public void insertClass(Clazz clazz) {
        // 先获取班主任即员工列表
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insertClass(clazz);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Clazz getClazzById(Integer id) {

       Clazz clazz = clazzMapper.getClazzById(id);

        return clazz;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperateLogAnnotation
    public void updateClass(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClass(clazz);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Clazz> getAllClazzInfo() {

        List<Clazz> list =  clazzMapper.getAllClazzInfo();
        return list;
    }

    @Override
    public List<Map> getClazzCount() {
      List<Map> list =  clazzMapper.getClazzCount();
        return list;
    }

    @Override
    public List<Map> getStudentDegreeData() {
        List<Map> list =   clazzMapper.getStudentDegreeData();
        return list;
    }


}
